package base.jdk8_demo.lambda.demo04;


import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Test;

/**
 * jdk7 和 jdk8 对集合数据实现排序的方式
 */
public class TestComparator {
    /**
     * jdk7 对集合数据实现排序
     */
    public static List<String> java7Sort(List<String> list){
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        return list;
    }

    /**
     * jdk8 对集合数据实现排序
     */
    public static List<String> java8Sort(List<String> list){
        Collections.sort(list , (o1,o2)-> o1.compareTo(o2));
        return list;
    }

    @Test
    public void test(){
        List<String> list = Arrays.asList("zs","ls","ww","zl","tq","zz");
        List<String> sort7 = java7Sort(list);
        System.out.println(sort7);
        System.out.println("-----------------------");

        list = Arrays.asList("zs","ls","ww","zl","tq","zz","hh");
        List<String> sort8 = java8Sort(list);

        System.out.println(sort8);
    }

}
